/*
 *  Copyright (c) 2009, Baturu.COM. All rights reserved.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package com.baturu.oss.facade;

import com.baturu.oss.dao.OpenDAOManager;
import com.baturu.oss.entities.posts.OpenPost;
import java.util.ArrayList;
import java.util.List;

/**
 * manages posts.
 * 
 * @author javafuns
 */
public class OpenPostFacade {

    public OpenPostFacade() {
    }

    /**
     * find posts.
     * 
     * @param batchSize
     * @param firstItem
     * @return
     */
    public List<OpenPost> findPosts(int batchSize, int firstItem) {
        OpenDAOManager odm = OpenDAOManager.newInstance();
        List<OpenPost> posts = new ArrayList<OpenPost>();

        odm.startTransaction();
        try {
            posts = odm.getPostDAO().findPosts(batchSize, firstItem, "postCreateDate desc");
            odm.commitTransaction();
            return posts;
        } finally {
            odm.close();
        }
    }

    /**
     * compute posts count.
     * 
     * @return
     */
    public int getPostCount() {
        OpenDAOManager odm = OpenDAOManager.newInstance();
        odm.startTransaction();
        try {
            int c = odm.getPostDAO().getPostCount();
            odm.commitTransaction();
            return c;
        } finally {
            odm.close();
        }
    }

    /**
     * Save post.
     * 
     * @param post
     * @return
     */
    public OpenPost savePost(OpenPost post) {
        OpenDAOManager odm = OpenDAOManager.newInstance();
        odm.startTransaction();
        try {
            OpenPost op = odm.getPostDAO().newPost(post);
            odm.commitTransaction();
            return op;
        } finally {
            odm.close();
        }
    }
}
